/**
 * Writed by yura and mak
 */

package wz.legend.handlers.auction;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import wz.legend.util.FileStorage;

import com.thoughtworks.xstream.XStream;

/**
 * User: yura
 */
public class BuyManager {
    private static final Logger LOG = Logger.getLogger(BuyManager.class);
    public static final String FILE_NAME = "config//buyconfig.xml";

    private FileStorage fileStorage;
    private List<BuyProduct> itemList = new ArrayList<BuyProduct>();

    public synchronized void saveList() {
        fileStorage.saveStream(itemList);
    }

    public synchronized void loadList() {
        itemList = (List<BuyProduct>) fileStorage.loadStream();
        if (itemList == null) {
            itemList = new ArrayList<BuyProduct>();
        }
    }

    public synchronized Lot findBuyItem(int minMoney, int money, List<Lot> lots) {
        int maxProfit = -1;
        Lot bestLot = null;
        for (Lot lot : lots) {
            if (lot.getCost().longValue() != 0) {
                int profit = calculateProfit(lot);
//                LOG.debug(lot.getProduct().getName() + " one cost = "
//                        + (int) (lot.getCost().longValue() / lot.getAmount())
//                        + " count = " + lot.getAmount() + " cost = "
//                        + lot.getCost() + " profit = " + profit);
                if (profit > maxProfit
                        && money - lot.getCost().intValue() >= minMoney) {
                    maxProfit = profit;
                    bestLot = lot;
                }
            }
        }
        return bestLot;
    }

    public synchronized void itemBuied(Lot lot) {
        BuyProduct buyProduct = findBuyProduct(lot);
        buyProduct.setAmount(buyProduct.getAmount() - lot.getAmount());
        saveList();
    }

    private int calculateProfit(Lot lot) {
        BuyProduct buyProduct = findBuyProduct(lot);
        if (buyProduct != null) {
            if (buyProduct.getQualityHi() <= lot.getQualityHi()
                    && buyProduct.getQualityLow() <= lot.getQualityLow()) {

                int lotPrice = lot.getCost().intValue()
                        / lot.getAmount().intValue();
                if (buyProduct.getAmount() >= lot.getAmount()
                        && lotPrice <= buyProduct.getCost().intValue()) {

                    return buyProduct.getCost().intValue()
                            * lot.getAmount().intValue()
                            - lot.getCost().intValue();
                }
            }
        }
        return -1;
    }

    private BuyProduct findBuyProduct(Lot lot) {
        for (BuyProduct buyProduct : itemList) {
            if (isSameProduct(lot, buyProduct)) {
                return buyProduct;
            }
        }
        return null;
    }

    private boolean isSameProduct(Lot lot, BuyProduct buyProduct) {
        return lot.getProduct().getName() != null
                && lot.getProduct().getLevel() != null
                && lot.getProduct().getName().toUpperCase().equals(
                        buyProduct.getName().toUpperCase())
                && lot.getProduct().getLevel().equals(buyProduct.getLevel());
    }

    public synchronized FileStorage getFileStorage() {
        return fileStorage;
    }

    public synchronized void setFileStorage(FileStorage fileStorage) {
        this.fileStorage = fileStorage;
    }
}
